<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Award
{
    protected $order;
    protected $user;

    // protected $set;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        // $is_repeat = ProductArea::checkRepeat($product_id);
        //
        // $is_new1 = ProductArea::checkNew1($product_id);
        // $is_new2 = ProductArea::checkNew2($product_id);

        // 新报单
        if ($is_bao) {
            $this->recommend();
        }


    }


    // 推荐奖
    public function recommend()
    {
        $award_name1 = '推荐奖';
        $award_type1 = 11;
        $award_name2 = '间荐奖';
        $award_type2 = 12;

        // $product_id = OrderItem::find()
        //     ->where(['order_id' => $this->order->id])
        //     ->select(['product_id'])
        //     ->column();
        //
        // $products = Product::find()
        //     ->where(['id' => $product_id])
        //     ->asArray()
        //     ->all();

        $items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->asArray()
            ->all();

        $p1 = 0;
        $p2 = 0;

        foreach ($items as $item) {
            $obj = $this->getCommissionByProduct($item['product_id']);
            if ($obj) {
                $p1 += $obj['r1'] * $item['quantity'];
                $p2 += $obj['r2'] * $item['quantity'];
            }
        }

        // foreach ($products as $product) {
        //     $obj = $this->getCommissionByProduct($product['id']);
        //     if ($obj) {
        //         $p1 += $obj['r1'];
        //         $p2 += $obj['r2'];
        //     }
        // }

        $r1 = Chain::getReferrer($this->order->user_id, true);
        if ($r1) {
            if ($r1->level >= UserLevel::$keys['vip1']) {
                $this->saveCommission($r1->id, $this->order->user_id, $this->order->id, $p1, $award_type1, $award_name1);
            }

            $r2 = Chain::getReferrer($r1->id, true);
            if ($r2) {
                if ($r2->level >= UserLevel::$keys['vip1']) {
                    $this->saveCommission($r2->id, $this->order->user_id, $this->order->id, $p2, $award_type2, $award_name2);
                }
            }

        }

    }

    // 根据产品, 获取佣金
    private function getCommissionByProduct($id)
    {
        return SetProductCommission::find()
            ->where(['product_id' => $id])
            ->one();
    }



    // 保存佣金
    protected function saveCommission($user_id, $fans_id, $order_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $fans_id;
            $model->order_id = $order_id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                $account->save();
            }
        }
    }
}